﻿module Sustainalytics.Event {

    import Common = Sustainalytics.Common;

    export class EventService implements IEventService {
        private $q: ng.IQService;
        private $http: ng.IHttpService;
        private $location: ng.ILocationService;
        private notificationFactory: Common.INotificationFactory;
        private staticObjects: Common.IStaticObjects;

        //Ctor.
        constructor($q: ng.IQService, $http: ng.IHttpService, $location: ng.ILocationService, notificationFactory: Common.INotificationFactory, staticObjects: Common.IStaticObjects) {
            this.$q = $q;
            this.$http = $http;
            this.$location = $location;
            this.notificationFactory = notificationFactory;
            this.staticObjects = staticObjects;
        }

        // show error message for a given error code
        private showError(err) {
            var ctx = this;
            if (err && err['code'])
                ctx.notificationFactory.error('', ctx.staticObjects.getErrorMesageByStatusCode(err, err['code']));
        }

        private showMessage(msg) {
            var ctx = this;
            ctx.notificationFactory.success(msg);
        }

        private eventsUrl(): string {
            return this.$location.protocol() + "://" + this.$location.host() + ":" + this.$location.port() + this.staticObjects.eventsServiceBaseUrl
                + "/api/v1/events";
        }

        public getEventsList(skip: number, take: number, orderBy: string[], orderAscending: boolean/*, startDate: Date, endDate: Date*/) {
            var ctx = this;
            var def = this.$q.defer();

            var query =
                {
                    term: null,
                    eventType: null,
                    skip: skip,
                    take: take,
                    includeAllFields: true,
                    getCount: false,
                    orderBy: orderBy,
                    orderAscending: orderAscending,
                    isPublished: true,
                    truncateBody: true
                    //#region events listed in Calendar
                    //startDateBegin: (startDate.getMonth() + 1) + "/" + startDate.getDate() + "/" + startDate.getFullYear(),
                    //startDateEnd: (endDate.getMonth() + 1) + "/" + endDate.getDate() + "/" + endDate.getFullYear(),
                    //#endregion
                };

            var url = ctx.eventsUrl() + "?" + $.param(query);
            var promise = this.$http.get(url);
            promise
                .success(function(data) {
                    def.resolve(data);
                })
                .error(function(err) {
                    ctx.showError(err);
                    def.reject();
                });

            return def.promise;
        }

        public getEventById(eventId: string): ng.IPromise<EventDto> {
            var ctx = this;
            var def = this.$q.defer();
            var url = ctx.eventsUrl() + "/" + eventId;
            var promise = this.$http.get(url);
            promise.success(function (data) { def.resolve(data); }).error(function (err) { ctx.showError(err); def.reject(); })
            return def.promise;
        }

        ////#region Events listed in Calendar
        //public getEventByDate(startDate: string): ng.IPromise<EventDto> {
        //    var ctx = this;
        //    var def = this.$q.defer();
        //    var url = ctx.eventsUrl() + "/date/" + startDate;

        //    var promise = this.$http.get(url);
        //    promise.success(function (data) { def.resolve(data); }).error(function (err) { ctx.showError(err); def.reject(); })
        //    return def.promise;
        //}
        ////#endregion Events listed in Calendar
    }

    // register service with Event Angular module
    angular.module('Sustainalytics.Event').service('EventService',
        ['$q', '$http', '$location', 'notificationFactory', 'staticObjects', EventService]);
}